(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.profAcctViewController', profAcctViewController);
	profAcctViewController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig' , 'authService', 'ajaxServiceFactory', 'validatorConfig'];
	function profAcctViewController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, authService, ajaxServiceFactory, validatorConfig) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("profAcctViewController"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getProfAcctOptions : {
				url : 'do/cda/dataList/profAcctOptions',
				method : 'post'
			},
		});
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.filterEventCodes = ctrl.dialogsModule.params.filterEventCodes || [];
		ctrl.prodType = ctrl.dialogsModule.params.prodType;
		ctrl.updateProcessFlag = ctrl.dialogsModule.params.updateProcessFlag;
		ctrl.profAcct =  {};
		ctrl.formName = "profAcctForm";
		
		ctrl.init = function() {
			ctrl.initGeneral().then(function() {
				ctrl.profAcct = ctrl.dialogsModule.params.profAcct || {};
				ctrl.profAcctOld = ctrl.dialogsModule.params.profAcctOld;
				if (ctrl.profAcctOld) {
					ctrl.profAcctOld.acctRules = ctrl.profAcctOld.acctRules || [];
					ctrl.profAcctOld.profSubAccts = ctrl.profAcctOld.profSubAccts || [];
				}
			});
		};
		
		ctrl.initGeneral = function() {
			var defer = $q.defer();
			
			var params = {};
			params.prodType = ctrl.prodType;
			getProfAcctOptions(params).then(function(response) {
				ctrl.eventCodeOptions = response.eventCodeOptions;
				ctrl.yesNoOptions = response.yesNoOptions;
				ctrl.categoryOptions = response.categoryOptions;
				defer.resolve();
			}, angular.noop);
			
			ctrl.acctRuleInnerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.acctRule.tableConfigs.table, ctrlAcctRuleRowSelectionChanged);
			ctrl.acctRuleInnerGridOptions.data = 'vm.profAcct.acctRules';
			ctrl.acctRuleInnerGridOptions.verifyData = 'vm.profAcctOld.acctRules';
			ctrl.profSubAcctInnerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.profSubAcct.tableConfigs.table, ctrlProfSubAcctRowSelectionChanged);
			ctrl.profSubAcctInnerGridOptions.data = 'vm.profAcct.profSubAccts';
			ctrl.profSubAcctInnerGridOptions.verifyData = 'vm.profAcctOld.profSubAccts';
			
			return defer.promise;
		};
		function getProfAcctOptions(params) {
			var defer = $q.defer();
			ajaxService.getProfAcctOptions(params).then(function(response) {
				var profAcctOptions = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
//						tempValue.data = value;
						tempValue = value;
						profAcctOptions[key] = tempValue;
					});
				}
				defer.resolve(profAcctOptions);
			});
			return defer.promise;
		}
		
		ctrl.eventCodeChanged = function(value) {
			if (ctrl.profAcct.eventCode) {
				for (var i = 0; i < ctrl.eventCodeOptions.length; i++) {
					var _event = ctrl.eventCodeOptions[i];
					if (_event.value == ctrl.profAcct.eventCode) {
						resetAcctByEventExtProperty(ctrl, _event);
						break;
					}
				}
			}
		};
		
		function resetAcctByEventExtProperty (ctrl , _event) {
			ctrl.profAcct.eventExtProperty = _event.extProperty;
			// has serviceSubEvents, mean has prof acct
			if (ctrl.profAcct.eventExtProperty.serviceSubEvents && ctrl.profAcct.eventExtProperty.serviceSubEvents.length > 0) {
				ctrl.profAcct.subeventFlag = 'Y';
			} else {
				ctrl.profAcct.subeventFlag = 'N';
			}
			
			ctrl.profAcct.crAcctType = ctrl.profAcct.eventExtProperty.glCrAcctType;
			ctrl.profAcct.drAcctType = ctrl.profAcct.eventExtProperty.glDrAcctType;
			
			// for init change row.entity readonly or not
			if (ctrl.profAcct.acctRules) {
				angular.forEach(ctrl.profAcct.acctRules, function(acctRule, key) {
					acctRule.glDrAcctFlag = ctrl.profAcct.eventExtProperty.glDrAcctFlag;
					acctRule.glCrAcctFlag = ctrl.profAcct.eventExtProperty.glCrAcctFlag;
				});
			} 
		}
		
		function ctrlAcctRuleRowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedAcctRules = selectedRows;
		}
		
		function ctrlProfSubAcctRowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedProfSubAccts = selectedRows;
		}
		
		ctrl.viewProfSubAcct = function(currRow) {		
			var params = {};
			// for show selected service on popup page
			var _selectedProfSubAcct = currRow.entity;
			params.profSubAcct = angular.copy(_selectedProfSubAcct);
			params.serviceSubEvents = ctrl.profAcct.eventExtProperty.serviceSubEvents;// for subAcct eventCode
			//
			if (ctrl.profAcctOld && ctrl.profAcctOld.profSubAccts) {
				var oldModel4SelectedRow;
				angular.forEach(ctrl.profAcctOld.profSubAccts, function(profSubAcct, index) {
					if (profSubAcct.subeventCode == params.profSubAcct.subeventCode) {
						oldModel4SelectedRow = profSubAcct;
					}
				});
				params.profSubAcctOld = oldModel4SelectedRow;
			}
			modalService.openCustomDialogs({
			   'title': 'Prof Sub Acct View',
			   'dialogsModule' : 'cda.profsubacct.popupview',
			   'params':params,
			   'btnCallback' : {
					'ok' : function(profSubAcct) {						
					}
				}		
			});
		};
		
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
	}
})(angular);
